package com.castle.fortress.admin.temp.service.impl;

import cn.hutool.core.util.StrUtil;
import com.castle.fortress.common.utils.ConvertUtil;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.castle.fortress.admin.temp.entity.TempTreeTableEntity;
import com.castle.fortress.admin.temp.dto.TempTreeTableDto;
import com.castle.fortress.admin.temp.mapper.TempTreeTableMapper;
import com.castle.fortress.admin.temp.service.TempTreeTableService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import java.util.List;
import com.castle.fortress.admin.utils.BizCommonUtil;

/**
 * 生成左树右表示例 服务实现类
 *
 * @author 
 * @since 2023-08-02
 */
@Service
public class TempTreeTableServiceImpl extends ServiceImpl<TempTreeTableMapper, TempTreeTableEntity> implements TempTreeTableService {
    /**
    * 获取查询条件
    * @param tempTreeTableDto
    * @return
    */
    public QueryWrapper<TempTreeTableEntity> getWrapper(TempTreeTableDto tempTreeTableDto){
        QueryWrapper<TempTreeTableEntity> wrapper= new QueryWrapper();
        if(tempTreeTableDto != null){
            TempTreeTableEntity tempTreeTableEntity = ConvertUtil.transformObj(tempTreeTableDto,TempTreeTableEntity.class);
            wrapper.like(StrUtil.isNotEmpty(tempTreeTableEntity.getName()),"name",tempTreeTableEntity.getName());
            wrapper.like(tempTreeTableEntity.getTreeId() != null,"tree_id",tempTreeTableEntity.getTreeId());
        }
        return wrapper;
    }


    @Override
    public IPage<TempTreeTableDto> pageTempTreeTable(Page<TempTreeTableDto> page, TempTreeTableDto tempTreeTableDto) {
        QueryWrapper<TempTreeTableEntity> wrapper = getWrapper(tempTreeTableDto);
        Page<TempTreeTableEntity> pageEntity = new Page(page.getCurrent(), page.getSize());
        Page<TempTreeTableEntity> tempTreeTablePage=baseMapper.selectPage(pageEntity,wrapper);
        Page<TempTreeTableDto> pageDto = new Page(tempTreeTablePage.getCurrent(), tempTreeTablePage.getSize(),tempTreeTablePage.getTotal());
        pageDto.setRecords(ConvertUtil.transformObjList(tempTreeTablePage.getRecords(),TempTreeTableDto.class));
        return pageDto;
    }

	@Override
	public IPage<TempTreeTableDto> pageTempTreeTableExtends(Page<TempTreeTableDto> page, TempTreeTableDto tempTreeTableDto){
		Map<String,Long> pageMap = BizCommonUtil.getPageParam(page);
          TempTreeTableEntity tempTreeTableEntity = ConvertUtil.transformObj(tempTreeTableDto,TempTreeTableEntity.class);
		List<TempTreeTableEntity> tempTreeTableList=baseMapper.extendsList(pageMap,tempTreeTableEntity);
		Long total = baseMapper.extendsCount(tempTreeTableEntity);
		Page<TempTreeTableDto> pageDto = new Page(page.getCurrent(), page.getSize(),total);
		pageDto.setRecords(ConvertUtil.transformObjList(tempTreeTableList,TempTreeTableDto.class));
		return pageDto;
	}
	@Override
	public TempTreeTableDto getByIdExtends(Long id){
          TempTreeTableEntity  tempTreeTableEntity = baseMapper.getByIdExtends(id);
		return ConvertUtil.transformObj(tempTreeTableEntity,TempTreeTableDto.class);
	}

    @Override
    public List<TempTreeTableDto> listTempTreeTable(TempTreeTableDto tempTreeTableDto){
        QueryWrapper<TempTreeTableEntity> wrapper = getWrapper(tempTreeTableDto);
        List<TempTreeTableEntity> list = baseMapper.selectList(wrapper);
        return ConvertUtil.transformObjList(list,TempTreeTableDto.class);
    }
}

